﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Process;
using CJAtech.BBS.Web.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;

namespace CJAtech.BBS.Web.Controllers
{
    public partial class BMController
    {
        /// <summary>
        /// 获取投票信息
        /// </summary>
        /// <param name="VoteHeadID"></param>
        /// <returns></returns>
        public ActionResult GetVoteInfo(int ArticleID)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            BizVoteHead voteHeadDao = new BizVoteHead(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            //BizCustomerVote customerVoteDao = new BizCustomerVote(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            ArticleInfo articleInfo = objBizArticleInfo.GetRecord(ArticleID);
            if (articleInfo == null)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "该文章不存在";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }

            List<VoteHead> VoteHeadList = voteHeadDao.GetList(" IsDel=0 and ArticleID=" + ArticleID);
            List<CJ_VoteInfo> list = new List<CJ_VoteInfo>();
            foreach (var vhead in VoteHeadList)
            {
                Guid VoteHeadID = vhead.VoteHeadID;
                CJ_VoteInfo head = new Models.CJ_VoteInfo();
                head.DetailList = new List<Models.CJ_VoteDetail>();
                var voteHead = voteHeadDao.GetRecord(new Guid(VoteHeadID.ToString()));
                StringBuilder strBuilder = new StringBuilder();

                strBuilder.Append("     select a.VoteDetailID ,VoteHeadID,ArticleID ,TextContent ,ResourceContent,IsDel ,CreateTime,UpdateTime,Num=case when b.Num is null then 0 else b.Num end  from (select * from BBS_VoteDetail_TB where IsDel=0) a left join ( select Num=count(1),VoteDetailID from BBS_CustomerVote_TB  group by VoteDetailID ) b on a.VoteDetailID=b.VoteDetailID  where VoteHeadID='" + VoteHeadID + "'");
                DataTable dt = voteHeadDao.DbContext.ExecuteDataTable(strBuilder.ToString(), null);

                head.VoteHeadID = voteHead.VoteHeadID;
                head.VoteTitle = voteHead.VoteTitle;
                head.MaxCheckNum = voteHead.MaxCheckNum;
                head.ArticleID = voteHead.ArticleID;
                head.ContentType = voteHead.ContentType;

                int totalCount = Convert.ToInt32(dt.Compute("sum(Num)", ""));
                int totalCount2 = Convert.ToInt32(dt.Compute("sum(ArticleID)", ""));
                decimal percentage = 0m;
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = dt.Rows[i];
                    CJ_VoteDetail d = new Models.CJ_VoteDetail();
                    d.VoteDetailID = new Guid(row["VoteDetailID"].ToString());
                    d.VoteHeadID = new Guid(row["VoteHeadID"].ToString());
                    d.TextContent = row["TextContent"].ToString();
                    d.ResourceContentList = row["ResourceContent"].ToString().Split(',').ToList();
                    d.VoteNum = Convert.ToInt32(row["Num"].ToString());
                    if (totalCount != 0)
                    {
                        if (i == dt.Rows.Count - 1)
                        {
                            d.VotePercentage = 100 - percentage;
                        }
                        else
                        {
                            d.VotePercentage = Convert.ToDecimal(d.VoteNum * 100) / totalCount;
                            d.VotePercentage = Convert.ToDecimal(d.VotePercentage.ToString("f2"));
                            percentage += d.VotePercentage;
                        }
                    }
                    head.DetailList.Add(d);
                }
                list.Add(head);
            }
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "";
            objInvokeResult.Data = list;
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用户投票
        /// </summary>
        /// <param name="VoteDetailDs"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SubmitVoteIDs(List<Guid> list)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            List<Guid> VoteDetailDs = list;
            if (VoteDetailDs == null || VoteDetailDs.Count == 0)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "投票数量为空";
                return Json(objInvokeResult, "text/plain");
            }
            BizVoteHead voteHeadDao = new BizVoteHead(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            BizVoteDetail voteDetailDao = new BizVoteDetail(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            BizCustomerVote customerVoteDao = new BizCustomerVote(CurrentBBSProcess.CurrentCommunityConfig.ConnectionStringKey, CurrentBBSProcess.CurrentCommunityConfig.DataObjectParam);
            VoteDetail detail = voteDetailDao.GetRecord(VoteDetailDs[0]);
            if (detail == null || detail.IsDel == true)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "投票选项错误";
                return Json(objInvokeResult, "text/plain");
            }

            VoteHead head = voteHeadDao.GetRecord(detail.VoteHeadID);
            if (head == null || head.IsDel == true)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "投票选项ID错误";
                return Json(objInvokeResult, "text/plain");
            }
            if (VoteDetailDs.Count > head.MaxCheckNum)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "超过限定勾选数量";
                return Json(objInvokeResult, "text/plain");
            }
            UserInfo objUserInfo = CurrentBBSProcess.GetUserInfo();
            List<CustomerVote> customerVoteList = customerVoteDao.GetList(" VoteHeadID='" + head.VoteHeadID + "' and CustomerID='" + objUserInfo.UserId + "'");
            if (customerVoteList != null && customerVoteList.Count > 0)
            {
                objInvokeResult.ResultCode = "Fail";
                objInvokeResult.ResultMessage = "对不起,您已投过票";
                return Json(objInvokeResult, "text/plain");
            }
            for (var i = 0; i < VoteDetailDs.Count(); i++)
            {
                CustomerVote cv = new CustomerVote();
                cv.ArticleID = head.ArticleID;
                cv.VoteHeadID = head.VoteHeadID;
                cv.VoteDetailID = VoteDetailDs[i];
                cv.CreateTime = DateTime.Now;
                cv.CustomerID = new Guid(objUserInfo.UserId);
                customerVoteDao.Add(cv);
            }
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "投票成功";
            return Json(objInvokeResult, "text/plain");
        }
    }
}
